﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace zmMoniter
{
    public partial class frm2D : DevExpress.XtraEditors.XtraForm
    {
        private Hashtable htDam = new Hashtable();
        bool    bFlash = false;
        #region **************上坝段屏幕参数**************
        private double m_xSStart;//存储屏幕左下角的实际坐标X
        private double m_ySStart;//存储屏幕左下角的实际坐标Y
        private Extent m_SExtent;
        private double m_SBlc;//存储屏幕转换比例尺
        private int m_wSScreen, m_hSScreen;//存储视图屏幕的实际宽度和高度（逻辑坐标）
        public int m_SMapMode;
        private double m_SMouseX, m_SMouseY;//鼠标移动轨迹
        private bool m_bSMove;//是否执行移动地图操作
        private bool m_bSZommIn;//是否执行放大地图操作
        private bool m_bSZommOut;//是否执行缩小地图操作
        public bool bSReady;//是否准备好数据了
        Point m_SPoint1, m_SPoint2;//移动时候的起终点坐标
        public float fSBasicHeight=3198;
        #endregion
        public mainForm main = null;
        #region **************下坝段屏幕参数**************
        private double m_xXStart;//存储屏幕左下角的实际坐标X
        private double m_yXStart;//存储屏幕左下角的实际坐标Y
        private Extent m_XExtent;
        private double m_XBlc;//存储屏幕转换比例尺
        private int m_wXScreen, m_hXScreen;//存储视图屏幕的实际宽度和高度（逻辑坐标）
        public int m_XMapMode;
        private double m_XMouseX, m_XMouseY;//鼠标移动轨迹
        private bool m_bXMove;//是否执行移动地图操作
        private bool m_bXZommIn;//是否执行放大地图操作
        private bool m_bXZommOut;//是否执行缩小地图操作
        public bool bXReady;//是否准备好数据了
        Point m_XPoint1, m_XPoint2;//移动时候的起终点坐标
        public float fXBasicHeight = 3314;
      
        #endregion
        private ArrayList alSGraph = new ArrayList();//上坝段图元数组

        private ArrayList alXGraph = new ArrayList();//下坝段图元数组
        private ArrayList alSelSGraph = new ArrayList();//选中上坝段图元数组
        private ArrayList alSelXGraph = new ArrayList();//选中下坝段图元数组
        public  frm2D(mainForm frm)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.ControlBox = false;
            bSReady = false;
            m_SExtent = new Extent();
            
            bXReady = false;
            m_XExtent = new Extent();
           // updateMapExtent();
            m_SMapMode = 4;
            m_XMapMode = 4;
            this.main = frm;
            bFlash = false;
        }
        #region **************图形数据文件读取**************
        public  void loadDamData()
        {
            clearGraph();
            loadSDamData();
            loadXDamData();
            getDeckElv();
            reSDraw();
            reXDraw();
        }
        private bool loadSDamData()
        {
            bool bResult = false;
            //异常检测开始
            try
            {
                FileStream fs = new FileStream(Application.StartupPath+"\\Data\\藏木大坝上下游坝块剖视图\\#-S.txt", FileMode.Open, FileAccess.Read);//读取文件设定
                StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.GetEncoding("UTF-8"));//设定读写的编码
                //使用StreamReader类来读取文件
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                //  从数据流中读取每一行，直到文件的最后一行
               
                string strLine = m_streamReader.ReadLine();
                float fmaxDamElv = -999999999;
                float fminDamElv = 999999999;
                float fmaxDamX = -999999999;
                float fminDamX = 999999999;
                while (strLine != null)
                {
                    string[] sStr = strLine.Split(':');
                    string sID = sStr[0];
                    string key =sID+ "-S";
                    Dam dam = new Dam(sID, "S");
                    string[] sPoints = sStr[1].Split(';');
                    float fminElv = 999999999;
                    float fmaxElv = -999999999;
                    float fminX = 999999999;
                    float fmaxX = -999999999;
                    Graph gObj = new Graph(1, 3);
                    gObj.setFlag(key);
                    gObj.setPen(new Pen(Color.Black, 2));
                    gObj.setFont(new Font("微软雅黑", 9f));
                    gObj.addText(sID);
                    for (int i = 0; i < sPoints.Length; i++)
                    {
                        string[] sXY = sPoints[i].Split(',');
                        if (sXY.Length == 2)
                        {
                           float damX = float.Parse(sXY[0]);
                           float damElv = float.Parse(sXY[1]);
                           gObj.addPoint(new Point((double)damX, (double)damElv));
                           addSGraph(gObj);
                           dam.addPoint(new Point((double)damX, (double)damElv));
                           
                           fmaxElv = fmaxElv > damElv ? fmaxElv : damElv;
                           fminElv = fminElv < damElv ? fminElv : damElv;
                           fminX = fminX > damX ? damX : fminX;
                           fmaxX = fmaxX > damX ? fmaxX : damX;
                           
                        }
                    }
                   
                    dam.setDamElv(fmaxElv);
                    fmaxDamElv = fmaxDamElv > fmaxElv ? fmaxDamElv : fmaxElv;
                    fminDamElv = fminDamElv < fminElv ? fminDamElv : fminElv;
                    fmaxDamX = fmaxDamX > fmaxX ? fmaxDamX : fmaxX;
                    fminDamX = fminDamX < fminX ? fminDamX : fminX;
                    htDam.Add(key, dam);
                    strLine = m_streamReader.ReadLine();
                }
                //int iElvCount=(fmaxDamElv-fminDamElv);
                float iElv = -9999;
                for (iElv = fminDamElv; iElv < fmaxDamElv; iElv += 10)
                {
                    Graph gObj = new Graph(1, 2);
                    
                    gObj.setPen(new Pen(Color.FromArgb(128, 128, 128, 128), 0.3f));
                    gObj.setPenStyle(DashStyle.Dot);
                    gObj.addPoint(new Point((double)fminDamX, (double)iElv));
                    gObj.addPoint(new Point((double)fmaxDamX, (double)iElv));
                    addSGraph(gObj);
                    
                    Graph gFontObj = new Graph(1, -1);
                    gFontObj.setFont(new Font("微软雅黑", 9f));
                    gFontObj.addPoint(new Point((double)fmaxDamX, (double)iElv));
                    gFontObj.addText((iElv + fSBasicHeight).ToString());
                    addSGraph(gFontObj);
                }
                if (iElv != fmaxDamElv)
                {
                    Graph gObj = new Graph(1, 2);
                    gObj.setPen(new Pen(Color.FromArgb(128, 128, 128, 128), 0.3f));
                    gObj.setPenStyle(DashStyle.Dot);

                    gObj.addPoint(new Point((double)fminDamX, (double)fmaxDamElv));
                    gObj.addPoint(new Point((double)fmaxDamX, (double)fmaxDamElv));
                    addSGraph(gObj);

                    Graph gFontObj = new Graph(1, -1);
                    gFontObj.setFont(new Font("微软雅黑", 9f));
                    gFontObj.addPoint(new Point((double)fmaxDamX, (double)fmaxDamElv));
                    gFontObj.addText((fmaxDamElv + fSBasicHeight).ToString());
                    addSGraph(gFontObj);
                }
                //关闭此StreamReader对象
                bResult = true;
               
            }
            catch
            {
                //抛出异常
                bResult = false;
            }

            return bResult;
        }
        private bool loadXDamData()
        {
            bool bResult = false;
            //异常检测开始
            try
            {
                
                FileStream fs = new FileStream(Application.StartupPath + "\\Data\\藏木大坝上下游坝块剖视图\\#-X.txt", FileMode.Open, FileAccess.Read);//读取文件设定
                StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.GetEncoding("UTF-8"));//设定读写的编码
                //使用StreamReader类来读取文件
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                //  从数据流中读取每一行，直到文件的最后一行
                
                string strLine = m_streamReader.ReadLine();
                float fmaxDamElv = -999999999;
                float fminDamElv = 999999999;
                float fmaxDamX = -999999999;
                float fminDamX = 999999999;
                while (strLine != null)
                {
                    string[] sStr = strLine.Split(':');
                    string sID = sStr[0];
                    string key = sID+"-X";
                    Dam dam = new Dam(sID, "X");
                    string[] sPoints = sStr[1].Split(';');
                    float fminElv = 999999999;
                    float fmaxElv = -999999999;
                    float fminX = 999999999;
                    float fmaxX = -999999999;
                    Graph gObj = new Graph(1, 3);
                    gObj.setFlag(key);
                    gObj.setPen(new Pen(Color.Black, 2));
                    gObj.setFont(new Font("微软雅黑", 9f));
                    gObj.addText(sID);
                    for (int i = 0; i < sPoints.Length; i++)
                    {
                        string[] sXY = sPoints[i].Split(',');
                        if (sXY.Length == 2)
                        {
                            float damX = float.Parse(sXY[0]);
                            float damElv = float.Parse(sXY[1]);
                            gObj.addPoint(new Point(double.Parse(sXY[0]), double.Parse(sXY[1])));
                            addXGraph(gObj);
                            dam.addPoint(new Point((double)damX, (double)damElv));

                            fmaxElv = fmaxElv > damElv ? fmaxElv : damElv;
                            fminElv = fminElv < damElv ? fminElv : damElv;
                            fminX = fminX > damX ? damX : fminX;
                            fmaxX = fmaxX > damX ? fmaxX : damX;
                        }
                    }
                    dam.setDamElv(fmaxElv);
                    fmaxDamElv = fmaxDamElv > fmaxElv ? fmaxDamElv : fmaxElv;
                    fminDamElv = fminDamElv < fminElv ? fminDamElv : fminElv;
                    fmaxDamX = fmaxDamX > fmaxX ? fmaxDamX : fmaxX;
                    fminDamX = fminDamX < fminX ? fminDamX : fminX;
                    htDam.Add(key, dam);
                    strLine = m_streamReader.ReadLine();
                }
                //int iElvCount=(fmaxDamElv-fminDamElv);
                float iElv = -9999;
                for (iElv = fminDamElv; iElv < fmaxDamElv; iElv += 10)
                {
                    Graph gObj = new Graph(1, 2);
                    gObj.setPen(new Pen(Color.FromArgb(128,128,128,128), 0.3f));
                    gObj.setPenStyle(DashStyle.Dot);

                    gObj.addPoint(new Point((double)fminDamX, (double)iElv));
                    gObj.addPoint(new Point((double)fmaxDamX, (double)iElv));
                    addXGraph(gObj);

                    Graph gFontObj = new Graph(1, -1);
                    gFontObj.setFont(new Font("微软雅黑", 9f));
                    gFontObj.addPoint(new Point((double)fmaxDamX, (double)iElv));
                    gFontObj.addText((iElv + fXBasicHeight).ToString());
                    addXGraph(gFontObj);
                    
                }
                
                //关闭此StreamReader对象
                //getDeckElv();
                bResult = true;
                
            }
            catch
            {
                //抛出异常
                bResult = false;
            }

            return bResult;
        }
        #endregion
        private void getDeckElv()
        {
            //Hashtable htDeck = new Hashtable();
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            string sSQL = "select 编号,仓面名称,坝段,上下游块,最低高程,最高高程,开仓状态 FROM 仓面信息 WHERE 开仓状态<>2 Order By 最低高程 DESC";// 
            DataTable dt = db.ReturnDataTable(sSQL);
            db.CloseConn();
            if (dt == null)//modify by lance 2013-1-20
            {
                this.main.siInfo.Caption = "数据库连接失败!";
                return;//modify by lance 2013-1-20
            }
            this.main.siInfo.Caption = "";
            int iCount = dt.Rows.Count;
            if (iCount == 0)
                return;
            for (int i = 0; i < iCount; i++)
            {
                DataRow dataRow = dt.Rows[i];
                string sID = dataRow["编号"].ToString().Trim();
                string sdamBlock = dataRow["上下游块"].ToString().Trim();
                string sdam = dataRow["坝段"].ToString().Trim();
                float fMinElv = float.Parse(dataRow["最低高程"].ToString().Trim());
                float fMaxElv = float.Parse(dataRow["最高高程"].ToString().Trim());
                int bOpenDeck = int.Parse(dataRow["开仓状态"].ToString().Trim());
                string key = sdam+"-" +sdamBlock;
                if (bOpenDeck == 1)//添加正在施工数据
                {
                    ArrayList pts=null;
                    if (!htDam.ContainsKey(key))
                        continue;
                    Dam dam = (Dam)htDam[key]; 
                    if (dam.sdamBlock == "S")
                    {
                         pts=getOpenDeckPoints(fMinElv, fMaxElv,fSBasicHeight,dam.alPoint);
                    }
                    else
                    {
                        pts = getOpenDeckPoints(fMinElv, fMaxElv, fXBasicHeight, dam.alPoint);
                    }
                    if (pts.Count<=3)
                        continue;
                    //需要绘制当前浇筑高度
                    Graph gObj = new Graph(1, 31);
                    gObj.setPen(new Pen(Color.Black, 2));
                    gObj.setBrush(new SolidBrush(Color.FromArgb(128, 255, 0, 0)));
                    for (int k = 0; k < pts.Count; k++)
                    {
                        Point pt = (Point)pts[k];
                        gObj.addPoint(pt);
                    }
                    if (dam.sdamBlock == "S")
                        addSGraph(gObj);
                    else
                        addXGraph(gObj);
                }
                getElv(sdamBlock, ref fMinElv, ref fMaxElv);
                
               
                if (htDam.ContainsKey(key))
                {
                   Dam value = (Dam)htDam[key];
                   if(fMinElv >= value.deck.fminElv)
                    {
                        Deck deck = value.deck;
                        deck.upDate(sID, sdamBlock, sdam, fMinElv, fMaxElv, bOpenDeck);
                    }
                }
            }

            foreach (DictionaryEntry objDE in htDam)
            {
                Dam dam = (Dam)objDE.Value;
                if (dam.deck.sdeckID != "-1" && dam.deck.fmaxElv != -999999999)
                {
                    //需要绘制当前浇筑高度
                    Graph gObj = new Graph(1, 31);
                    gObj.setPen(new Pen(Color.Black, 2));
                   
                    gObj.setBrush(new SolidBrush(Color.FromArgb(128, 0, 0, 255)));
                    ArrayList alpts=getUnderDamPoints(dam.deck.fminElv,dam.alPoint);
                    for (int i = 0; i < alpts.Count; i++)
                    {
                        Point pt = (Point)alpts[i];
                        gObj.addPoint(pt);
                    }
                    if (dam.sdamBlock == "S")
                        addSGraph(gObj);
                    else
                        addXGraph(gObj);
                    
                }
            }
        }

        private void getElv(string sdamBlock, ref float fMinElv, ref float fMaxElv)//相对高程转为实际高程
        {
            if (sdamBlock == "S")//添加已施工数据
            {
                fMinElv = fMinElv - fSBasicHeight;
                fMaxElv = fMaxElv - fSBasicHeight;
            }
            else
           {
                fMinElv = fMinElv - fXBasicHeight;
                fMaxElv = fMaxElv - fXBasicHeight;
            }
        }
        private ArrayList getOpenDeckPoints(float fMinElv, float fMaxElv,float fBasicHeight,ArrayList damPoints)
        {
            ArrayList alPts = new ArrayList();
            for (int i = 0; i < (damPoints.Count - 1); i++)
            {
                Point pt1 = (Point)damPoints[i];
                Point pt2 = (Point)damPoints[i+1];
                double ptElv1 = pt1.dY + fBasicHeight;
                double ptElv2 = pt2.dY + fBasicHeight;
                if (ptElv1 <= fMaxElv && ptElv1 >= fMinElv)
                {
                    alPts.Add(pt1);
                }
                if (ptElv1 > fMaxElv && ptElv2 < fMaxElv &&ptElv2>fMinElv)
                {
                   alPts.Add(getTntersectPt(pt1, pt2, fMaxElv - fBasicHeight));
                }
                else if ((ptElv1 > fMaxElv) && (ptElv2 < fMinElv))
                {
                    alPts.Add(getTntersectPt(pt1, pt2, fMaxElv - fBasicHeight));
                    alPts.Add(getTntersectPt(pt1, pt2, fMinElv - fBasicHeight));
                }
                
                if (ptElv1 < fMaxElv && ptElv2 >fMaxElv &&ptElv1>fMinElv)
                {
                   alPts.Add(getTntersectPt(pt1, pt2, fMaxElv - fBasicHeight));
                }
                else if ((ptElv2 > fMaxElv) && (ptElv1 < fMinElv))
                {
                    alPts.Add(getTntersectPt(pt1, pt2, fMinElv - fBasicHeight));
                    alPts.Add(getTntersectPt(pt1, pt2, fMaxElv - fBasicHeight));
                }

               
                if (i == (damPoints.Count - 1))
                {
                    if (ptElv2 <= fMaxElv && ptElv2 >= fMinElv)
                    {
                        alPts.Add(pt2);
                    }
                }
            }
            if (alPts.Count >= 3)
                alPts.Add(alPts[0]);
            else
                alPts.Clear();
            return alPts;
        }
        private ArrayList getUnderDamPoints(float fMinElv, ArrayList damPoints)
        {
            ArrayList alPts = new ArrayList();
            for (int i = 0; i < (damPoints.Count - 1); i++)
            {
                Point pt1 = (Point)damPoints[i];
                Point pt2 = (Point)damPoints[i + 1];
                double ptElv1 = pt1.dY ;
                double ptElv2 = pt2.dY ;
                if (ptElv1 <= fMinElv && ptElv2 <= fMinElv)
                {
                    alPts.Add(pt1);
                    alPts.Add(pt2);
                }
                else if (ptElv1 <= fMinElv && ptElv2 > fMinElv)
               {
                    alPts.Add(pt1);
                    alPts.Add(getTntersectPt(pt1, pt2, fMinElv));
                }
                else if (ptElv2 <= fMinElv && ptElv1 > fMinElv)
                {
                    alPts.Add(getTntersectPt(pt1, pt2, fMinElv));
                    alPts.Add(pt2);
                }
                else if (ptElv2 > fMinElv && ptElv1 > fMinElv)
                {
                    alPts.Add(new Point(pt1.dX, fMinElv));
                    alPts.Add(new Point(pt2.dX, fMinElv));
                }
            }
            if (alPts.Count >= 3)
                alPts.Add(alPts[0]);
            else
                alPts.Clear();
            return alPts;
        }
        private Point getTntersectPt(Point pt1, Point pt2, double fElv)
        {
           if(pt1.dX==pt2.dX)
               return new Point(pt1.dX,fElv);
           double dX = (pt2.dX - pt1.dX) / (pt2.dY - pt1.dY) * (fElv - pt1.dY) + pt1.dX;
           return new Point(dX, fElv);
        }
        private void setSCanvas(double dX, double dY, double dBlc)
        {
            m_xSStart = dX;
            m_ySStart = dY;
            m_SBlc = dBlc;
        }
        private void updateMapExtent()
        {
            m_wSScreen = this.SCanvas.Width;
            m_hSScreen = this.SCanvas.Height;
            if (m_wSScreen != 0 && m_hSScreen != 0)
            {
                bSReady = true;
               
                int iScreenSize = m_wSScreen > m_hSScreen ? m_hSScreen : m_wSScreen;
                double dMapWSize = m_SExtent.dMaxX - m_SExtent.dMinX;
                double dMapHSize = m_SExtent.dMaxY - m_SExtent.dMinY;
                double dMapSize = dMapWSize > dMapHSize ? dMapWSize : dMapHSize;
                m_SBlc =0.5*dMapSize / iScreenSize;

                if (m_wSScreen > m_hSScreen)
                {
                    m_xSStart = (m_SExtent.dMaxX + m_SExtent.dMinX) / 2 - m_wSScreen / 2 * m_SBlc;
                    m_ySStart = (m_SExtent.dMaxY + m_SExtent.dMinY) / 2 - m_hSScreen / 2 * m_SBlc;
                }
                else
                {
                    m_xSStart = (m_SExtent.dMaxX + m_SExtent.dMinX) / 2 - m_wSScreen / 2 * m_SBlc;
                    m_ySStart = (m_SExtent.dMaxY + m_SExtent.dMinY) / 2 - m_hSScreen / 2 * m_SBlc;
                }
            }

            m_wXScreen = this.XCanvas.Width;
            m_hXScreen = this.XCanvas.Height;
            if (m_wXScreen != 0 && m_hXScreen != 0)
            {
                bXReady = true;
              

                int iScreenSize = m_wXScreen > m_hXScreen ? m_hXScreen : m_wXScreen;
                double dMapWSize = m_XExtent.dMaxX - m_XExtent.dMinX;
                double dMapHSize = m_XExtent.dMaxY - m_XExtent.dMinY;
                double dMapSize = dMapWSize > dMapHSize ? dMapWSize : dMapHSize;
                m_XBlc = 0.5 * dMapSize / iScreenSize;

                if (m_wXScreen > m_hXScreen)
                {
                    m_xXStart = (m_XExtent.dMaxX + m_XExtent.dMinX) / 2 - m_wXScreen / 2 * m_XBlc;
                    m_yXStart = (m_XExtent.dMaxY + m_XExtent.dMinY) / 2 - m_hXScreen / 2 * m_XBlc;
                }
                else
                {
                    m_xXStart = (m_XExtent.dMaxX + m_XExtent.dMinX) / 2 - m_wXScreen / 2 * m_XBlc;
                    m_yXStart = (m_XExtent.dMaxY + m_XExtent.dMinY) / 2 - m_hXScreen / 2 * m_XBlc;
                }
            }
        }
        private void addSGraph(Graph gObj)
        {
            alSGraph.Add(gObj);
        }
        private void addXGraph(Graph gObj)
        {
            alXGraph.Add(gObj);
        }
        private void clearGraph()
        {
            alSGraph.Clear();
            alXGraph.Clear();
            alSelSGraph.Clear();
            alSelXGraph.Clear();
            htDam.Clear();
        }
   
        #region **************上坝段坐标转换操作函数**************
        public int DLtoVL_S(double l)
        {
            return (int)(l/m_SBlc );                                //返回实际坐标表示的长度值
        }
        public double VLtoDL_S(int l)
        {
            return (double)(l*m_SBlc);                             //返回逻辑坐标表示的长度值
        }
        public void VPtoDP_S(int x, int y, ref double X, ref double Y) //将逻辑坐标转换成实际坐标
        {
            X = m_xSStart + x * m_SBlc;
            Y = m_ySStart + m_SBlc * (m_hSScreen - y);
        }
        public double VPtoDPX_S(int x) //将逻辑坐标转换成实际坐标
        {
            return m_xSStart + x * m_SBlc;
        }
        public double VPtoDPY_S(int y) //将逻辑坐标转换成实际坐标
        {
            return m_ySStart + m_SBlc * (m_hSScreen - y);
        }
        public void DPtoVP_S(double x, double y, ref int X, ref int Y) //将实际坐标换成逻辑坐标转
        {
            X = (int)((x - m_xSStart) / m_SBlc);
            Y = m_hSScreen - (int)((y-m_ySStart) / m_SBlc);
            //Y = (int)((m_yStart - y) / blc);
        }
        public int DPtoVPX_S(double x) //将实际坐标换成逻辑坐标转
        {
            return (int)((x - m_xSStart) / m_SBlc);
        }
        public int DPtoVPY_S(double y) //将实际坐标换成逻辑坐标转
        {
            return m_hSScreen - (int)((y - m_ySStart) / m_SBlc);
            //return (int)((m_yStart - y) / blc);
        }
        #endregion
        #region **************下坝段坐标转换操作函数**************
        public int DLtoVL_X(double l)
        {
            return (int)(m_XBlc * l);                                //返回实际坐标表示的长度值
        }
        public double VLtoDL_X(int l)
        {
            return (double)(l / m_XBlc);                             //返回逻辑坐标表示的长度值
        }
        public void VPtoDP_X(int x, int y, ref double X, ref double Y) //将逻辑坐标转换成实际坐标
        {
            X = m_xXStart + x * m_XBlc;
            Y = m_yXStart + m_XBlc * (m_hXScreen - y);
        }
        public double VPtoDPX_X(int x) //将逻辑坐标转换成实际坐标
        {
            return m_xXStart + x * m_XBlc;
        }
        public double VPtoDPY_X(int y) //将逻辑坐标转换成实际坐标
        {
            return m_yXStart + m_XBlc * (m_hXScreen - y);
        }
        public void DPtoVP_X(double x, double y, ref int X, ref int Y) //将实际坐标换成逻辑坐标转
        {
            X = (int)((x - m_xXStart) / m_XBlc);
            Y = m_hXScreen - (int)((m_yXStart - y) / m_XBlc);
            //Y = (int)((m_yStart - y) / blc);
        }
        public int DPtoVPX_X(double x) //将实际坐标换成逻辑坐标转
        {
            return (int)((x - m_xXStart) / m_XBlc);
        }
        public int DPtoVPY_X(double y) //将实际坐标换成逻辑坐标转
        {
            return m_hXScreen - (int)((y-m_yXStart) / m_XBlc);
            //return (int)((m_yStart - y) / blc);
        }
        #endregion

        private void reSDraw()
        {
            //m_wSScreen = this.SCanvas.Width;
           // m_hSScreen = this.SCanvas.Height;
            m_SExtent.reSet();
            for (int i = 0; i < alSGraph.Count; i++)
            {
                Graph gObj = (Graph)alSGraph[i];
                m_SExtent.upDate(gObj.extent);
            }
            SCanvas.Refresh();
            
        }
        private void reXDraw()
        {
           // m_wXScreen = this.XCanvas.Width;
            //m_hXScreen = this.XCanvas.Height;
            m_XExtent.reSet();
            for (int i = 0; i < alXGraph.Count; i++)
            {
                Graph gObj = (Graph)alXGraph[i];
                m_XExtent.upDate(gObj.extent);
            }
            XCanvas.Refresh();

        }
        private void frm2D_Resize(object sender, EventArgs e)
        {
            //刷新界面 
            SCanvas.Refresh();
            XCanvas.Refresh();
        }
        private void SCanvas_Paint(object sender, PaintEventArgs e)
        {
            updateMapExtent();
            if (!bSReady)
                return;
            Graphics g = e.Graphics; //创建画板,这里的画板是由Form提供的.
            g.Clear(Color.White);

            
            Font font = new Font("微软雅黑", 14f);
            PointF pointF = new PointF(m_wSScreen/2-100,30);
            SizeF sizeF = g.MeasureString("（左岸）上坝块浇筑高度（右岸）", font);
            
            ;
             //g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));
            g.DrawString("（左岸）上坝块浇筑高度（右岸）", font, Brushes.Black, pointF);
            font.Dispose();
           
            for (int i = 0; i < alSGraph.Count; i++)
            {
                Graph gObj = (Graph)alSGraph[i];
                switch (gObj.type)
                {
                    case -1://文字
                        {
                            g.DrawString(gObj.sText, gObj.font, Brushes.Black,
                                DPtoVPX_S(gObj.getPointX(0)), DPtoVPY_S(gObj.getPointY(0)) - 8);
                            break;
                        }
                    case 0://点
                        {


                            break;
                        }
                    case 1://直线
                        {

                            g.DrawLine(gObj.pen, DPtoVPX_S(gObj.getPointX(0)), DPtoVPY_S(gObj.getPointY(0)),
                                 DPtoVPX_S(gObj.getPointX(1)), DPtoVPY_S(gObj.getPointY(1)));//在画板上画直线,起始坐标为(10,10),终点坐标为(100,100)
                            break;
                        }
                    case 2://线
                        {
                            int iLength = gObj.alPoint.Count;
                            System.Drawing.Point[] pts = new System.Drawing.Point[iLength];
                            for (int j = 0; j < iLength; j++)
                            {
                                int iVPX = DPtoVPX_S(gObj.getPointX(j));
                                int iVPY = DPtoVPY_S(gObj.getPointY(j));
                                pts[j] = new System.Drawing.Point(iVPX, iVPY);
                            }
                            g.DrawLines(gObj.pen, pts);
                            pts = null;
                            break;
                        }
                    case 3://面 
                        {
                            int iLength = gObj.alPoint.Count;
                            System.Drawing.Point[] pts = new System.Drawing.Point[iLength];
                            for (int j = 0; j < iLength; j++)
                            {
                                int iVPX = DPtoVPX_S(gObj.getPointX(j));
                                int iVPY = DPtoVPY_S(gObj.getPointY(j));
                                pts[j] = new System.Drawing.Point(iVPX, iVPY);
                            }
                            
                            g.DrawPolygon(gObj.pen, pts);
                            if (gObj.sText != "" && gObj.font!=null)
                                g.DrawString(gObj.sText, gObj.font, Brushes.Black,
                                    DPtoVPX_S((gObj.extent.dMaxX + gObj.extent.dMinX) / 2.0-5),
                                    DPtoVPY_S(gObj.extent.dMaxY+10));


                            if (bFlash )
                            {
                                if (gObj.sFlag != "" && this.main.htWarnDeck.ContainsKey(gObj.sFlag))
                                {
                                    Brush brsh = Brushes.Red;
                                    g.FillPolygon(brsh, pts, FillMode.Winding);//填充的
                                }
                            }
                            pts = null;
                            break;
                        }
                    case 31://填充面 
                        {
                            
                            int iLength = gObj.alPoint.Count;
                            System.Drawing.Point[] pts = new System.Drawing.Point[iLength];
                            for (int j = 0; j < iLength; j++)
                            {
                                int iVPX = DPtoVPX_S(gObj.getPointX(j));
                                int iVPY = DPtoVPY_S(gObj.getPointY(j));
                                pts[j] = new System.Drawing.Point(iVPX, iVPY);
                            }
                            g.DrawPolygon(gObj.pen, pts);
                           
                            
                            g.FillPolygon(gObj.opaqueBrush, pts, FillMode.Alternate);
                            
                            pts = null;
                            break;
                        }
                    case 4://矩形
                        {

                            g.DrawRectangle(gObj.pen, DPtoVPX_S(gObj.getPointX(0)), DPtoVPY_S(gObj.getPointX(0)),
                                DLtoVL_S(gObj.getPointX(1)), DLtoVL_S(gObj.getPointX(1)));//在画板上画矩形,起始坐标为(10,10),宽为100,高为100
                            break;
                        }
                    case 5://圆
                        {

                            g.DrawEllipse(gObj.pen, DPtoVPX_S(gObj.getPointX(0)), DPtoVPX_S(gObj.getPointX(0)),
                                 DLtoVL_S(gObj.getPointX(1)), DLtoVL_S(gObj.getPointX(1)));//在画板上画椭圆,起始坐标为(10,10),外接矩形的宽为,高为
                            break;
                        }
                    default:
                        break;
                }
            }
            
            for (int i = 0; i < alSelSGraph.Count; i++)
            {
                Graph gSelObj = (Graph)alSelSGraph[i];
                if (gSelObj.type == 3)
                {
                    int iLength = gSelObj.alPoint.Count;
                    System.Drawing.Point[] pts = new System.Drawing.Point[iLength];
                    for (int j = 0; j < iLength; j++)
                    {
                        int iVPX = DPtoVPX_S(gSelObj.getPointX(j));
                        int iVPY = DPtoVPY_S(gSelObj.getPointY(j));
                        pts[j] = new System.Drawing.Point(iVPX, iVPY);
                    }
                    //Brush brsh = Brushes.Red;
                    //g.FillPolygon(brsh, pts, FillMode.Winding);//填充的

                    g.DrawPolygon(new Pen(Color.Red, 2), pts);
                    pts = null;
                }
            }
           
        }
        private void XCanvas_Paint(object sender, PaintEventArgs e)
        {
            if (!bXReady)
                return;

            updateMapExtent();

            Graphics g = e.Graphics; //创建画板,这里的画板是由Form提供的.
            g.Clear(Color.White);
            Font font = new Font("微软雅黑", 14f);
            PointF pointF = new PointF(m_wSScreen / 2 - 100, 30);
            SizeF sizeF = g.MeasureString("下坝块浇筑高度", font);
            g.DrawString("（左岸）下坝块浇筑高度（右岸）", font, Brushes.Black, pointF);
            font.Dispose();

            for (int i = 0; i < alXGraph.Count; i++)
            {
                Graph gObj = (Graph)alXGraph[i];
                switch (gObj.type)
                {
                    case -1://文字
                        {

                            g.DrawString(gObj.sText, gObj.font, Brushes.Black,
                                DPtoVPX_X(gObj.getPointX(0)), DPtoVPY_X(gObj.getPointY(0)) - 8);
                            break;
                        }
                    case 0://点
                        {


                            break;
                        }
                    case 1://直线
                        {

                            g.DrawLine(gObj.pen, DPtoVPX_X(gObj.getPointX(0)), DPtoVPY_X(gObj.getPointY(0)),
                                 DPtoVPX_X(gObj.getPointX(1)), DPtoVPY_X(gObj.getPointY(1)));//在画板上画直线,起始坐标为(10,10),终点坐标为(100,100)
                            break;
                        }
                    case 2://线
                        {
                            int iLength = gObj.alPoint.Count;
                            System.Drawing.Point[] pts = new System.Drawing.Point[iLength];
                            for (int j = 0; j < iLength; j++)
                            {
                                int iVPX = DPtoVPX_X(gObj.getPointX(j));
                                int iVPY = DPtoVPY_X(gObj.getPointY(j));
                                pts[j] = new System.Drawing.Point(iVPX, iVPY);
                            }
                            g.DrawLines(gObj.pen, pts);
                            pts = null;
                            break;
                        }
                    case 3://面 
                        {
                            int iLength = gObj.alPoint.Count;
                            System.Drawing.Point[] pts = new System.Drawing.Point[iLength];
                            for (int j = 0; j < iLength; j++)
                            {
                                int iVPX = DPtoVPX_X(gObj.getPointX(j));
                                int iVPY = DPtoVPY_X(gObj.getPointY(j));
                                pts[j] = new System.Drawing.Point(iVPX, iVPY);
                            }
                            g.DrawPolygon(gObj.pen, pts);
                            if (gObj.sText != "" && gObj.font != null)
                                g.DrawString(gObj.sText, gObj.font, Brushes.Black,
                                    DPtoVPX_X((gObj.extent.dMaxX + gObj.extent.dMinX) / 2.0 - 5),
                                    DPtoVPY_X(gObj.extent.dMaxY + 10));

                            if (bFlash)
                            {
                                if (gObj.sFlag != "" && this.main.htWarnDeck.ContainsKey(gObj.sFlag))
                                {
                                    Brush brsh = Brushes.Red;
                                    g.FillPolygon(brsh, pts, FillMode.Winding);//填充的
                                }
                            }
                            pts = null;
                            break;
                        }
                    case 31://填充面 
                        {

                            int iLength = gObj.alPoint.Count;
                            System.Drawing.Point[] pts = new System.Drawing.Point[iLength];
                            for (int j = 0; j < iLength; j++)
                            {
                                int iVPX = DPtoVPX_X(gObj.getPointX(j));
                                int iVPY = DPtoVPY_X(gObj.getPointY(j));
                                pts[j] = new System.Drawing.Point(iVPX, iVPY);
                            }
                            g.DrawPolygon(gObj.pen, pts);
                          
                            g.FillPolygon(gObj.opaqueBrush, pts, FillMode.Alternate);

                            pts = null;
                            break;
                        }
                    case 4://矩形
                        {

                            g.DrawRectangle(gObj.pen, DPtoVPX_X(gObj.getPointX(0)), DPtoVPY_X(gObj.getPointX(0)),
                                DLtoVL_X(gObj.getPointX(1)), DLtoVL_X(gObj.getPointX(1)));//在画板上画矩形,起始坐标为(10,10),宽为100,高为100
                            break;
                        }
                    case 5://圆
                        {

                            g.DrawEllipse(gObj.pen, DPtoVPX_X(gObj.getPointX(0)), DPtoVPY_X(gObj.getPointX(0)),
                                 DLtoVL_X(gObj.getPointX(1)), DLtoVL_X(gObj.getPointX(1)));//在画板上画椭圆,起始坐标为(10,10),外接矩形的宽为,高为
                            break;
                        }
                    default:
                        break;
                }
            }
            for (int i = 0; i < alSelXGraph.Count; i++)
            {
                Graph gSelObj = (Graph)alSelXGraph[i];
                if (gSelObj.type == 3)
                {
                    int iLength = gSelObj.alPoint.Count;
                    System.Drawing.Point[] pts = new System.Drawing.Point[iLength];
                    for (int j = 0; j < iLength; j++)
                    {
                        int iVPX = DPtoVPX_X(gSelObj.getPointX(j));
                        int iVPY = DPtoVPY_X(gSelObj.getPointY(j));
                        pts[j] = new System.Drawing.Point(iVPX, iVPY);
                    }
                    //Brush brsh = Brushes.Red;
                    //g.FillPolygon(brsh, pts, FillMode.Winding);
                    g.DrawPolygon(new Pen(Color.Red, 2), pts);
                    pts = null;

                }
            }

        }
        private void SCanvas_MouseDown(object sender, MouseEventArgs e)
        {
            if (!bSReady)
                return;
            alSelSGraph.Clear();
            double dX = VPtoDPX_S(e.X);
            double dY = VPtoDPY_S(e.Y);
            for (int i = 0; i < alSGraph.Count; i++)
            {
                Graph gObj = (Graph)alSGraph[i];
                if (gObj.extent.inExtent(dX, dY))
                {
                    if (IsPointInRegion((float)dX, (float)dY, gObj))
                    {
                        //gObj.setPen(new Pen(Color.Red,2));
                        //gObj.pen.Brush=
                        alSelSGraph.Add(gObj);
                        this.main.setDateByBlock(gObj.sFlag);
                    }
                }
            }
            SCanvas.Refresh();
        }
        private void XCanvas_MouseDown(object sender, MouseEventArgs e)
        {
            if (!bXReady)
                return;
            alSelXGraph.Clear();
            double dX = VPtoDPX_X(e.X);
            double dY = VPtoDPY_X(e.Y);
            for (int i = 0; i < alXGraph.Count; i++)
            {
                Graph gObj = (Graph)alXGraph[i];
                if (gObj.extent.inExtent(dX, dY))
                {
                    if (IsPointInRegion((float)dX, (float)dY, gObj))
                    {
                        alSelXGraph.Add(gObj);
                        this.main.setDateByBlock(gObj.sFlag);
                    }
                }
            }
            XCanvas.Refresh();
        }
        public  void FlashWarning(bool flash)
        {
            bFlash = flash;
            SCanvas.Refresh();
            XCanvas.Refresh();
        }
        public  bool IsPointInRegion(float dX, float dY, Graph gObj)
        {
            using(GraphicsPath myGraphicsPath = new GraphicsPath())
            {
                using (Region myRegion = new Region())
                {
                    myGraphicsPath.Reset();
                    int iLength=gObj.alPoint.Count;
                    System.Drawing.PointF[] pts = new System.Drawing.PointF[iLength];
                    for (int j = 0; j < iLength; j++)
                    {
                        float dTX = (float)gObj.getPointX(j);
                        float dTY = (float)gObj.getPointY(j);
                        pts[j] = new System.Drawing.PointF(dTX, dTY);
                    }
                    myGraphicsPath.AddPolygon(pts);
                    myRegion.MakeEmpty();
                    myRegion.Union(myGraphicsPath);
                    System.Drawing.PointF ptf = new System.Drawing.PointF(dX,dY);

                    bool bInRgn = myRegion.IsVisible(ptf);
                    
                    return bInRgn;
                }
            }
        }
       
    }
}
